package com.vito.oa.act.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.service.OaActApproverServer;
/**
 * 审批人控制类
 * @author xuezhennan
 *
 */
@Controller
@RequestMapping("oa/act/approver")
public class OaActApproverAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(OaActApproverAction.class);
	@Reference
	private OaActApproverServer oaActApproverServer;
	
	/**
	 * 获取企业该流程设置的审批人列表
	 * @param processKey
	 * @param des
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryProcessApproversInfo")
	public Result queryApproversInfoByProcessKey(String processKey,String des){
		Result rs = new Result();
		try{
			UserModel user =SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			String startUserId = user.getUserId();
			String currentDeptId = user.getDeptId();
			String topDeptId = user.getDeptParentId();
			Map<String,Object> auditors = null;
			boolean hasSet = true;
			if(oaActApproverServer.queryDefinedApproversCount(processKey, topDeptId) > 0){
				 auditors = oaActApproverServer.queryDefinedApprovers(processKey, startUserId, currentDeptId, topDeptId);
			}else if(oaActApproverServer.queryBranchCount(processKey, topDeptId) > 0){
				String branchId = oaActApproverServer.queryProcessBranch(processKey, topDeptId, des);
				auditors = oaActApproverServer.queryDefinedApprovers(processKey, startUserId, currentDeptId, topDeptId, branchId);
 			}
			if(auditors == null){
				auditors = new HashMap<>();
				hasSet = false;
			}
			auditors.put("hasSet", hasSet);
			rs.setCode(Result.CODE_OK);
			rs.setData(auditors);
		}catch(Exception e){
			log.error("查询审批人异常",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
		return rs;
	}
	
	/**
	 * 获取流程所有节点
	 * @param processKey
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryProcessAllNode")
	public Result queryProcessAllNode(String processKey){
		Result rs = new Result();
		try{
			UserModel user =SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if(Validata.isNullOrEmpty(processKey)){
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("参数错误");
				return rs;
			}
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActApproverServer.queryProcessAllNode(processKey,user.getDeptParentId()));
		}catch(Exception e){
			log.error("查询流程节点异常",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
		return rs;
	}
	/**
	 * 获取下一级节点
	 * @param processKey
	 * @param currentNodeId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryNextNode")
	public Result queryNextNode(String processKey,String currentNodeId){
		Result rs = new Result();
		try{
			UserModel user =SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if(Validata.isNullOrEmpty(processKey) || Validata.isNullOrEmpty(currentNodeId)){
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("参数错误");
				return rs;
			}
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActApproverServer.queryNextNode(processKey, currentNodeId));
		}catch(Exception e){
			log.error("查询流程节点异常",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
		return rs;
	}
	/**
	 * 获取企业职务/角色列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryRoleByDept")
	public Result queryRoleByDept(@RequestParam Map<String,Object> param){
		Result rs = new Result();
		try{
			UserModel user =SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			param.put("deptTop", user.getDeptParentId());
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActApproverServer.queryRoleByDept(param));
		}catch(Exception e){
			log.error("设置审批人-查询职务异常",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
		return rs;
	}
}
